package cn.itcast.nems.oldems.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.base.api.SubjectApi;
import cn.itcast.nems.base.dao.entity.Subject;
import cn.itcast.nems.base.dao.mapper.SubjectMapper;
import cn.itcast.nems.base.dto.SubjectDTO;
import cn.itcast.nems.base.dto.SubjectQueryDTO;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.employee.dao.bean.EmployeeGroupBean;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectApi {
    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public Page<SubjectDTO> findPageList(PageQueryModel<SubjectQueryDTO> queryModel) {
        Assert.notNull(queryModel, "参数为空");
        SubjectQueryDTO queryDTO = Objects.nonNull(queryModel.getCondition()) ? queryModel.getCondition() : new SubjectQueryDTO();
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Subject> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        page.setCurrent(queryModel.getPageNumber());
        page.setSize(queryModel.getPageSize());

        Set<String> subjectIds = new HashSet<>();
        if (queryDTO.getPermission() != null && queryDTO.getPermission()) {
            if (!AccountHolder.hasEmployeeGroups()) {
                return Page.of(queryModel.getPageNumber(), queryModel.getPageSize(), 0, new ArrayList<>());
            }
            List<EmployeeGroupBean> groupList = AccountHolder.getEmployeeGroups();
            if (groupList != null) {
                for (EmployeeGroupBean employeeGroupBean : groupList) {
                    for (String subjectId : employeeGroupBean.getSubjectIdList()) {
                        subjectIds.add(subjectId);
                    }
                }
                if (subjectIds.isEmpty()) {
                    return Page.of(queryModel.getPageNumber(), queryModel.getPageSize(), 0, new ArrayList<>());
                }
            }
        }
        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<Subject>()
                .eq(StringUtils.hasText(queryDTO.getId()), Subject::getId, queryDTO.getId())
                .in(!subjectIds.isEmpty(), Subject::getId, subjectIds)
                .like(StringUtils.hasText(queryDTO.getName()), Subject::getName, queryDTO.getName())
                .eq(StringUtils.hasText(queryDTO.getModel()), Subject::getModel, queryDTO.getModel())
                .eq(Objects.nonNull(queryDTO.getStatus()), Subject::getStatus, queryDTO.getStatus());
        IPage<Subject> result = getBaseMapper().selectPage(page, queryWrapper);
        List<SubjectDTO> list = null;
        if (Objects.nonNull(result) && CollectionUtil.isNotEmpty(result.getRecords())) {
            list = result.getRecords().stream().map(item -> CustomBeanUtil.copyProperties(item, new SubjectDTO())).collect(Collectors.toList());
        }
        return Page.of(queryModel.getPageNumber(), queryModel.getPageSize(), Long.valueOf(result.getTotal()).intValue(), list);
    }

    @Override
    public List<?> findSubjectsByTeachingModel(String model, Boolean status) {
        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<Subject>()
                .eq(StringUtils.hasText(model), Subject::getModel, model).eq(Objects.nonNull(status), Subject::getStatus, status);
        return subjectMapper.selectList(queryWrapper);
    }

    @Override
    public SubjectDTO findById(String id) {
        var subject = subjectMapper.selectById(id);
        if (subject != null) {
            var dto = CustomBeanUtil.copyProperties(subject, new SubjectDTO());
            return dto;
        }
        return null;
    }

    @Override
    public List<SubjectDTO> findListByIds(Collection<String> ids) {
        return
                subjectMapper.selectList(new QueryWrapper<Subject>()
                                .lambda()
                                .in(Subject::getId, ids))
                        .stream()
                        .map(subject -> CustomBeanUtil.copyProperties(subject, new SubjectDTO()))
                        .toList();
    }
}
